const uuid = require('uuid')

import md5 from 'js-md5';

// 全站加速站点的域名。
const DomainName = 'https://api.emo.yingling88.cn'

// 鉴权密钥。
const auth_key = 'Jm*toLhHPTB6m^rj&g';


const GetUrlRelativePath = (url) => {
    let relUrl = ''
    if (url.includes('//')) {
        let arrUrl = url.split("//");
        let start = arrUrl[1].indexOf("/");
        relUrl = arrUrl[1].substring(start); //stop省略，截取从start开始到结尾的所有字符
    } else {
        let start = url.indexOf("/");
        relUrl = url.substring(start)
    }

    if (relUrl.includes("?")) {
        relUrl = relUrl.split("?")[0];
    }

    return relUrl;
}

const url = (url) => {
    // 时间戳
    let time = Math.round(new Date().getTime() / 1000) + 3
    // 随机数
    let rand = uuid.v4().split("-").join("")
    // 实际回源访问的URL
    let Filename = GetUrlRelativePath(url)
    let sstring = `${Filename}-${time}-${rand}-0-${auth_key}`
    let _md5 = md5(sstring)
    let url_param = `auth_key=${time}-${rand}-0-${_md5}`
    return `${DomainName}${Filename}?${url_param}`
}
export default url